package com.byxy.managerial.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.alibaba.fastjson.JSON;
import com.byxy.managerial.service.UserService;
import com.opensymphony.xwork2.ActionContext;

public class LoginAction {

	private String username;
	private String password;

	@Resource
	private UserService userService;
	//登录
	public void login() throws IOException {
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		Map<String, Object> map = new HashMap<String, Object>();
		boolean isLogin = userService.login(username, password);
		if (isLogin) {
			map.put("message", true);
		} else {
			map.put("message", "用户名或密码错误，请重新输入");
		}
		PrintWriter pw = response.getWriter();
		String str = JSON.toJSONString(map);
		pw.write(str);
		pw.flush();
		pw.close();
	}
	//退出登录
	public String outLogin() {
		ActionContext.getContext().getSession().clear();
		return "success";
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

}
